﻿Imports Microsoft.VisualBasic

Public MustInherit Class CSearchPage : Inherits CPage

#Region "Querystring (Filters)"
    Public ReadOnly Property Search As String
        Get
            Return CWeb.RequestStr("search")
        End Get
    End Property
    Public ReadOnly Property SpeakerId As Integer
        Get
            Return CWeb.RequestInt("speak")
        End Get
    End Property
    Public ReadOnly Property LanguageId As Integer
        Get
            Return CWeb.RequestInt("lang")
        End Get
    End Property
#End Region

#Region "Common Controls"
    Protected MustOverride ReadOnly Property TopPanel As PlaceHolder
    Protected MustOverride ReadOnly Property SubPanel As PlaceHolder
    Protected Overridable ReadOnly Property TypeId As EProductType
        Get
            Return EProductType.All
        End Get
    End Property
#End Region

#Region "Menu Links"
    Private Sub CSearchPage_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
        TopMenuLink(CUtilities.NameAndCount("Collections", CProduct.Cache.Active.ConferenceSets), CSitemap.ConferenceSets(Nothing, SpeakerId, LanguageId), TypeId = EProductType.ConferenceSet)
        TopMenuLink(CUtilities.NameAndCount("Sermons", CProduct.Cache.Active.Sermons), CSitemap.Sermons(Nothing, SpeakerId, LanguageId), TypeId = EProductType.Sermon)
        TopMenuLink(CUtilities.NameAndCount("Manuals", CProduct.Cache.Active.Manuals), CSitemap.Manuals, TypeId = EProductType.Manual)
        TopMenuLink(CUtilities.NameAndCount("Paperbacks", CProduct.Cache.Active.Paperbacks), CSitemap.Paperbacks, TypeId = EProductType.Paperback)
        TopMenuLink("Tags", "~/tags.aspx", Request.RawUrl.Contains("tags.aspx"))

        'TopMenuLink(EProductType.All) 'Treat as Set+Sermon+Manual (if searching from any of those; no search for paperbacks; specific search for audio etc)
        'TopMenuLink(CUtilities.NameAndCount("Speakers", CSpeaker.Cache.HavingProducts), "javascript:", False)

        'Select Case TypeId
        '    Case EProductType.Manual, EProductType.Paperback : Exit Sub
        'End Select

        Dim u As String = Request.RawUrl.ToLower

        SubMenuLink(CUtilities.NameAndCount("Audio", CProduct.Cache.Active.Sermons.HavingMp3), CSitemap.Audio(Nothing, SpeakerId, LanguageId), u.Contains("audio.aspx"))
        SubMenuLink(CUtilities.NameAndCount("Video", CProduct.Cache.Active.Sermons.HavingVideos), CSitemap.Video(Nothing, SpeakerId, LanguageId), u.Contains("video.aspx"))
        SubMenuLink(CUtilities.NameAndCount("eBooks/Kindle", CProduct.Cache.Active.HavingEpub), CSitemap.Ebooks(Nothing, SpeakerId), u.Contains("ebooks.aspx")) 'sets, then standalone
        SubMenuLink(CUtilities.NameAndCount("Transcripts", CProduct.Cache.Active.Sermons.HavingTranscripts), CSitemap.Transcripts(Nothing, SpeakerId), u.Contains("transcripts.aspx") OrElse u.Contains("chinese.aspx"))
        SubMenuLink(CUtilities.NameAndCount("Sermon Notes", CProduct.Cache.Active.Sermons.HavingNotes), CSitemap.Notes(Nothing, SpeakerId), u.Contains("notes.aspx"))
        'SubMenuLink(CUtilities.NameAndCount("Art", CProduct.Cache.Active.Sermons.Art), CSitemap.Art(Nothing, SpeakerId), u.Contains("art.aspx"))

        'SubMenuLink(CUtilities.NameAndCount("Speakers", CSpeaker.Cache.HavingProducts), "javascript:", False)

        'todo: tags, speakers as mullet-cloud
        Dim chineseTranscripts As Boolean = Request.RawUrl.ToLower.Contains("chinese.aspx")
        If LanguageId > 0 OrElse chineseTranscripts Then
            Dim meta As New HtmlMeta()
            meta.HttpEquiv = "content-language"
            If LanguageId = ELanguage.Chinese OrElse chineseTranscripts Then
                meta.Content = "zh-sg"
                Page.Header.Controls.Add(meta)
            ElseIf LanguageId = ELanguage.Hindi Then
                meta.Content = "hi"
                Page.Header.Controls.Add(meta)
            End If
        End If
    End Sub

    Protected Sub TopMenuLink(i As EProductType)
        TopMenuLink(CProductType.Cache.PlusAll.GetById(i))
    End Sub
    Protected Sub TopMenuLink(i As CProductType)
        If i.TypeId = 0 Then
            TopMenuLink(i.PluralAndActive, CSitemap.StoreForSpeaker(0), TypeId = i.TypeId)
        Else
            TopMenuLink(i.PluralAndActive, CSitemap.Store(i.TypeId), TypeId = i.TypeId)
        End If
    End Sub

    Protected Sub TopMenuLink(name As String, url As String, selected As Boolean)
        MenuLink(TopPanel, name, url, selected)
    End Sub
    Protected Sub SubMenuLink(name As String, url As String, selected As Boolean)
        MenuLink(SubPanel, name, url, selected)
    End Sub

    Protected Function MenuLink(plh As Control, name As String, url As String, selected As Boolean, tooltip As String) As HyperLink
        MenuLink = MenuLink(plh, name, url, selected)
        MenuLink.ToolTip = tooltip
    End Function
    Protected Function MenuLink(plh As Control, name As String, url As String, selected As Boolean) As HyperLink
        Dim lnk As New HyperLink
        lnk.Text = name
        lnk.NavigateUrl = url
        If selected Then lnk.CssClass = "selected" Else lnk.CssClass = "menu"
        plh.Controls.Add(lnk)
        Return lnk
    End Function

#End Region

#Region "Side Links"
    Protected Sub AddBr(ctrl As Control)
        Dim br As New HtmlGenericControl("span")
        br.InnerHtml = "<br/>"
        ctrl.Controls.Add(br)
    End Sub
    Protected Overridable Function SideLinkUrl(t As CProductList.CTagFreq) As String
        Return CSitemap.Sermons(t.Tag, 0)
    End Function
    Protected Overridable Function SideLinkName(t As CProductList.CTagFreq) As String
        Return CUtilities.NameAndCount(t.Tag, CProduct.Cache.Active.Sermons.Search(t.Tag))
    End Function
    Protected Function SideLink(ctrl As Control, t As CProductList.CTagFreq) As HyperLink
        SideLink = SideLink(ctrl, SideLinkName(t), SideLinkUrl(t), "Search for: " & t.Tag)
        If t.Tag = Search Then MarkSelected(SideLink)
    End Function
    Protected Function SideLink(ctrl As Control, lang As CLanguage) As HyperLink
        SideLink = SideLink(ctrl, lang.NativeAndCountActive, CSitemap.Sermons(Nothing, 0, lang.LanguageId), lang.NativeAndName & " sermons")
        If lang.LanguageId = LanguageId Then MarkSelected(SideLink)
        SideLink.Font.Size = New FontUnit("larger")
        SideLink.Style.Add("margin-bottom", "5px")
    End Function
    Protected Function SideLink(ctrl As Control, s As CSpeaker) As HyperLink
        SideLink = SideLink(ctrl, s.NameAndCountActive, CSitemap.Sermons(Nothing, s.SpeakerId), s.SpeakerName & " sermons")
        If s.SpeakerId = SpeakerId Then MarkSelected(SideLink)
        If s.SpeakerId = ESpeaker.ShaneWillard OrElse s.SpeakerId = ESpeaker.MikeConnell Then
            SideLink.Style.Add("margin-top", "2px")
            SideLink.Style.Add("margin-bottom", "2px")
        End If
    End Function
    Protected Function SideLink(ctrl As Control, name As String, url As String, tooltip As String) As HyperLink
        Dim lnk As New HyperLink
        lnk.Text = name
        lnk.NavigateUrl = url
        lnk.ToolTip = tooltip
        lnk.Style.Add("display", "block")
        ctrl.Controls.Add(lnk)
        Return lnk
    End Function
    Protected Sub MarkSelected(lnk As HyperLink)
        lnk.Font.Bold = True
        lnk.Font.Underline = True
        lnk.ForeColor = Drawing.Color.Black
    End Sub
#End Region


End Class
